package com.cindata

import com.cindata.dictionary.ImportTypeDict
import grails.converters.JSON
import grails.gorm.transactions.Transactional
import org.springframework.security.access.annotation.Secured

import static org.springframework.http.HttpStatus.*

@Secured(['isAuthenticated()'])
@Transactional
class RepaymentImportController
{

    static allowedMethods = [save: "POST", update: "PUT", delete: "DELETE"]

    @Transactional
    def index(Integer max)
    {
        def order = params["order"]
        def sort = params["sort"]
        params.max = Math.min(max ?: 10, 100)
        def importFileType = ImportTypeDict.findByCode("Repayment")
        String sql = " from estateImportBatch e where e.importFileType.id = ${importFileType.id}"
        if (order && sort)
        {
            sql += " order by e.${sort} ${order}"
        }
        else
        {
            sql += " order by e.batchNumber desc "
        }
        def repaymentImportList = RepaymentImport.executeQuery(sql, [max: params.max ?: 10, offset: params.offset ?: 0])
        respond RepaymentImport.list(params), model: [repaymentImportCount: RepaymentImport.count(), repaymentImportBatchList: repaymentImportList, params: params]
    }

    @Transactional
    def searchImport()
    {
        def order = params["order"]
        def sort = params["sort"]
        def queryParam = new HashMap()
        String sql = "from RepaymentImport e where  1=1  "
        if (params["id"])
        {
            def id = Long.parseLong(params["id"])
            sql += "and e.batch.id = :id "
            queryParam["id"] = id
        }
        if (params["batchs"])
        {
            sql += "and e.batch.batchNumber like :batchs "
            queryParam["batchs"] = "%" + params["batchs"] + "%"
        }
        if (params["createBy"])
        {
            sql += "and e.createBy.username = :createBy "
            queryParam["createBy"] = params["createBy"]
        }
        if (params["serialNumber"])
        {
            sql += "and e.serialNumber like :serialNumber "
            queryParam["serialNumber"] = "%" + params["serialNumber"] + "%"
        }
        if (params["repaymentType"])
        {
            sql += "and e.repaymentType.id = :repaymentType "
            queryParam["repaymentType"] = Long.parseLong(params["repaymentType"])
        }

        //执行查询方法
        def count = RepaymentImport.executeQuery("select count(e.id) " + sql, queryParam)
        def repaymentImportList
        if (count[0] > 0)
        {
            params.max = params.max ?: 10
            params.offset = params.offset ?: 0
            if (order && sort)
            {
                sql += " order by e.${sort} ${order}"
            }
            else
            {
                sql += " order by e.id desc "
            }
            repaymentImportList = RepaymentImport.executeQuery(sql, queryParam, [max: params.max ?: 10, offset: params.offset ?: 0])
        }
        if (repaymentImportList != null)
        {
            respond RepaymentImport, model: [repaymentImportList: repaymentImportList, repaymentImportCount: count[0], params: params], view: 'index'
        }
        else
        {
            respond RepaymentImport, view: 'index'
        }
    }

    def show(RepaymentImport repaymentImport)
    {
        respond repaymentImport
    }

    def create()
    {
        respond new RepaymentImport(params)
    }

    @Transactional
    def save(RepaymentImport repaymentImport)
    {
        if (repaymentImport == null)
        {
            transactionStatus.setRollbackOnly()
            notFound()
            return
        }

        if (repaymentImport.hasErrors())
        {
            transactionStatus.setRollbackOnly()
            respond repaymentImport.errors, view: 'create'
            return
        }

        repaymentImport.save flush: true

        request.withFormat {
            form multipartForm {
                flash.message = message(code: 'default.created.message', args: [message(code: 'repaymentImport.label', default: 'RepaymentImport'), repaymentImport.id])
                redirect repaymentImport
            }
            '*' { respond repaymentImport, [status: CREATED] }
        }
    }

    def edit(RepaymentImport repaymentImport)
    {
        respond repaymentImport
    }

    @Transactional
    def update(RepaymentImport repaymentImport)
    {
        if (repaymentImport == null)
        {
            transactionStatus.setRollbackOnly()
            notFound()
            return
        }

        if (repaymentImport.hasErrors())
        {
            transactionStatus.setRollbackOnly()
            respond repaymentImport.errors, view: 'edit'
            return
        }

        repaymentImport.save flush: true

        request.withFormat {
            form multipartForm {
                flash.message = message(code: 'default.updated.message', args: [message(code: 'repaymentImport.label', default: 'RepaymentImport'), repaymentImport.id])
                redirect repaymentImport
            }
            '*' { respond repaymentImport, [status: OK] }
        }
    }

    @Transactional
    def delete(RepaymentImport repaymentImport)
    {

        if (repaymentImport == null)
        {
            transactionStatus.setRollbackOnly()
            notFound()
            return
        }

        repaymentImport.delete flush: true

        request.withFormat {
            form multipartForm {
                flash.message = message(code: 'default.deleted.message', args: [message(code: 'repaymentImport.label', default: 'RepaymentImport'), repaymentImport.id])
                redirect action: "index", method: "GET"
            }
            '*' { render status: NO_CONTENT }
        }
    }

    protected void notFound()
    {
        request.withFormat {
            form multipartForm {
                flash.message = message(code: 'default.not.found.message', args: [message(code: 'repaymentImport.label', default: 'RepaymentImport'), params.id])
                redirect action: "index", method: "GET"
            }
            '*' { render status: NOT_FOUND }
        }
    }
}
